/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.fastaprocessing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Kenneth
 */
public class FastaProcessing {

    /**
     * This method ensures only sequences within the specified interval are
     * retained. It will also remove linebreaks and stopcodons (signalled by *)
     * in the translated nucleotide fasta
     *
     * @param file the fasta file
     * @param minimalPeptideLength the minimum sequence length that should be
     * retained
     * @param maximalPeptideLength the maximum sequence length that should be
     * retained
     * @throws IOException
     * @throws Exception
     */
    public static void removeStopCodonsAndLineBreaks(File file, int minimalPeptideLength, int maximalPeptideLength) throws IOException, Exception {
        if (minimalPeptideLength < 1) {
            throw new Exception("Invalid minimal peptide length, please verify that this value is higher than 1");
        }
        if (maximalPeptideLength < 1 || maximalPeptideLength < minimalPeptideLength) {
            throw new Exception("Invalid minimal peptide length, please verify that this value is higher than the minimalPeptideLength");
        }
        File tempFasta = File.createTempFile(file.getName(), "temp.fasta");
        try (BufferedReader br = new BufferedReader(new FileReader(file)); BufferedWriter bw = new BufferedWriter(new FileWriter(tempFasta))) {
            String header = "";
            String sequence = "";
            boolean doContinue = true;
            for (String line; (line = br.readLine()) != null;) {
                if (line.startsWith(">")) {
                    //print the previous entry and reset
                    if (!header.isEmpty() && !sequence.isEmpty()) {
                        if (sequence.length() > minimalPeptideLength && sequence.length() <= maximalPeptideLength) {
                            bw.write(header + System.lineSeparator());
                        }
                        bw.write(sequence + System.lineSeparator());
                        bw.flush();
                        sequence = "";
                    }
                    header = line;
                    doContinue = true;
                } else if (doContinue) {
                    if (line.contains("*")) {
                        line = line.substring(0, line.indexOf("*"));
                        doContinue = false;
                    }
                    sequence += line;
                }
            }
            bw.flush();
        }
        //copy tempFile into the original one
        FileUtils.copyFile(tempFasta, file, true);
        tempFasta.delete();
    }
}
